﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ElfTyping.General;
using Microsoft.Xna.Framework;

namespace ElfTyping.States.GameState.Entities
{
    public class WordEntity : HealthEntity
    {
        public string Word { get; protected set; }
        public int NextLetterIndex { get; private set; }
        public char NextLetter
        {
            get
            {
                return (NextLetterIndex < Word.Length) ? Word[NextLetterIndex] : '\0';
            }
        }

        protected Color TypedLetterColor = Color.Red;
        protected Color UntypedLetterColor = Color.Black;

        DateTime? LastMistakeTime = null;
        DateTime? LastFirstLetter = null;
        public bool HasRecentMistake
        {
            get
            {
                return (LastMistakeTime != null) && ((LastFirstLetter != null) && (LastMistakeTime.Value - LastFirstLetter.Value).TotalSeconds < 5);
            }
        }

        public WordEntity(GameScript script, Microsoft.Xna.Framework.Vector2 center) : base(script, center) { }

        public void ResetNextLetter()
        {
            if (NextLetterIndex != 0)
            {
                LastMistakeTime = DateTime.Now;
            }

            NextLetterIndex = 0;
        }

        /// <summary>
        /// Tells the enemy that the next letter has been pressed.
        /// </summary>
        /// <returns>Whether the word has been finished.</returns>
        public virtual AcceptLetterResult AcceptLetter()
        {
            if (NextLetterIndex == 0)
            {
                LastFirstLetter = DateTime.Now;
            }

            NextLetterIndex++;
            if (NextLetterIndex == Word.Length)
            {                
                AcceptLetterResult returnValue = HasRecentMistake ? AcceptLetterResult.WordFinished : AcceptLetterResult.WordFinishedPerfect;
                OnWordFinish();
                
                return returnValue;
            }
            return AcceptLetterResult.Failed;
        }

        public enum AcceptLetterResult
        {
            Failed,
            WordFinished,
            WordFinishedPerfect
        }

        protected virtual void OnWordFinish()
        {
            if (!string.IsNullOrEmpty(Word))
            {
                Word = GetNewWord(Word.Length);
            }
            NextLetterIndex = 0;
            LastFirstLetter = null;
            LastMistakeTime = null;
        }

        protected string GetNewWord(int wordLength)
        {
            return DictionaryManager.GetManager().GetRandomWord(wordLength, false);
        }

        public override void Draw(Camera camera)
        {
            camera.MakeText(Word)
                    .WithColor(TypedLetterColor)
                    .WithSecondaryColor(UntypedLetterColor, NextLetterIndex)
                    .WithCenteredPosition(CenterPosition.X, CenterPosition.Y - DrawBox.Height * 0.7f)
                    .Draw();

            base.Draw(camera);
        }
    }
}
